﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using UmbracoMVCDemo.Models.Shared;

namespace UmbracoMVCDemo.Classes.Mappers
{
    internal static class NavigationItemMappers
    {
        /// <summary>
        /// Maps IPublishedContent to a simple navigation item.
        /// </summary>
        /// <typeparam name="TModel">Type of model, to map to. Must be or inherit from NavigationItemModel</typeparam>
        /// <param name="model">Model to map</param>
        /// <param name="content">Content to map from.</param>
        /// <param name="currentPage">The current page, used to set active flag</param>
        /// <returns>The mapped model</returns>
        /// <remarks>
        /// Note, that this mapper, does not map children, nor does it take a list of items.
        /// It only maps a single item. Children must be selected in the controller. As the query code can vary.
        /// </remarks>
        internal static TModel Map<TModel>(TModel model, IPublishedContent content, IPublishedContent currentPage)
            where TModel : NavigationItemModel
        {
            model.Name = content.GetPropertyValue<string>("title");
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                model.Name = content.Name;
            }

            model.Url = content.Url;

            model.Active = currentPage.IsDescendantOrSelf(content);

            return model;
        }
    }
}